<?php

namespace app\controller;

use app\BaseController;
use Wechat\Config;
use EasyWeChat\Factory;

class Wechat extends BaseController
{
    // easywechat实例
    public $wechat;
    // openid
    public $openid;
    // 公众号名
    public $title = 'ImgHelper';
    // 初始化数据
    public function __construct()
    {
        $this->wechat = Factory::officialAccount([
            'app_id' => Config::$appid,
            'secret' => Config::$appsecret,
            'token' => Config::$token,
            'response_type' => 'array',
        ]);
        // 提交域名验证时 请先注释
        $message = $this->wechat->server->getMessage();
        $this->openid = $message['FromUserName'];
    }

    /**
     * 数据分发
     *
     * @return void
     */
    public function index()
    {
        $this->wechat->server->push(function ($message) {
            switch ($message['MsgType']) {
                case 'event':
                    switch ($message['Event']) {
                        case 'subscribe':
                            return $this->subscribe();
                            break;
                        case 'unsubscribe':
                            return $this->unsubscribe();
                            break;
                        case 'CLICK':
                            return $this->click($message['EventKey']);
                            break;
                            // ... 其他事件
                        default:
                            return 'unknow event type' . $message['Event'];
                    }
                    break;
                case 'text':
                    return $this->text($message['Content']);
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                    // ... 其它消息
                default:
                    return 'unknow msg type' . $message['MsgType'];
                    break;
            }
        });
        // 消息响应
        ($this->wechat->server->serve())->send();
    }

    /**
     * 文本处理
     *
     * @param [type] $text
     * @return void
     */
    protected function text($text)
    {
        $textInfo = explode('@', $text);
        if ($key = Config::cmdKey(strtolower($textInfo[0]))) {
            $val = isset($textInfo[1]) ? $textInfo[1] : '';
            $role = Config::$cmd[$key]['role'];
        } else { // 分发默认操作
            $key = Config::cmdKey(strtolower($text)) ?: 'text';
            $val = ($key == 'text') ? $text : '';
            $role = Config::$cmd[$key]['role'];
        }
        return Config::cmdAction($val, $key, $this->openid, $role);
    }

    /**
     * 点击消息
     */
    protected function click($key)
    {
        switch ($key) {
            default:
                return "感谢关注" . $this->title;
                break;
        }
    }

    /**
     * 关注
     *
     * @return void
     */
    protected function subscribe()
    {
        return "感谢关注" . $this->title . " (v3.0)\n本公众号非营销公众号，纯属程序员的娱乐玩具，会不定期开发更新一些实用小功能\n并不会一天到晚推送不实用的营销文章\nso~关注了就留着呗，说不定有用得着的地方，嘻嘻~\n\n" . '输入“帮助”或“help”可查看<a href="https://mp.weixin.qq.com/mp/homepage?__biz=MzU2Njc4NjI0NA==&hid=1&sn=8f907ed9a66c40c29fcb963a77a95e79">帮助手册</a>';
    }

    /**
     * 取消关注
     */
    protected function unsubscribe()
    {
        // TODO
    }
}
